package sdk3.components.remoteService
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	import mx.controls.Label;
	import mx.controls.TextArea;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.HTTPService;

	public class readXMLconfig extends EventDispatcher
	{
		public var STATUS:TextArea = new TextArea();
		
		public var READ_CONFIG:HTTPService = new HTTPService();
		
		/** the config data that was read at last **/
		public var config:Object = null;
		
		/** the url/path to the config file, if it is in the same folder as this readXMLconfig.as just give the name without any path **/
		public var config_url:String = '';

		public function readXMLconfig(STATUS:TextArea = null,url:String = '')
		{
			if(STATUS != null)
			{
				this.STATUS = STATUS;
			}
			this.config_url = url;
		}

		public function read(event:Event = null):void
		{
			if(STATUS != null)
			{
				this.STATUS = STATUS;
			}
			if(event == null)
			{
				/** 1. call url **/
				READ_CONFIG.url = config_url;
				this.STATUS.text = 'reading config file: '+this.config_url;;
				READ_CONFIG.addEventListener(ResultEvent.RESULT, read);
				READ_CONFIG.addEventListener(FaultEvent.FAULT, read);
				READ_CONFIG.send();
			}
			else
			{
				if(event.type == 'result')
				{
					this.STATUS.text = 'reading config.xml successful.';
					config = READ_CONFIG.lastResult;
				}
				else
				{
					try
					{
						this.STATUS.text = 'reading  '+this.config_url+' failed: '+event['fault']['faultCode'];
					}
					catch(e:Error)
					{
						this.STATUS.text = 'reading '+this.config_url+' failed without returning an faultCode.';
					}
					trace(this.STATUS.text);
				}

				this.dispatchEvent(event);
			}
		}
	}
}